The string manipulation functions strncat
, strlcat
and strlcpy
require a size argument that describes how
many bytes from the source buffer are used at most. In many situations the size of the source buffer is unknown, which is why the size argument for
these functions should be based on the size of the destination buffer. This helps to prevent buffer overflows.
Note that strncat
always adds a terminating null character at the end of the appended characters; therefore, the size argument should
be smaller than the size of the destination to leave enough space for the null character.
#include <stdio.h>
#include <string.h>
void foo(const char *src) {
char dst[10] = {0};
strlcpy(dst, src, sizeof(src)); // Noncompliant: size of destination should be used.
printf("%s", dst);
}